// components/setText/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            default: false
        },
        title: {
            type: String,
            default: '设置分类'
        },
        text: {
            type: String,
            default: ''
        },
        img: {
            type: String,
            default: ''
        },
        isImg: {
            type: Boolean,
            default: false
        },
        type: {
            type: String,
            default: 'input'
        },
        tips: {
            type: String,
            default: '请输入内容'
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        newText: '',
        newImg: ''
    },
    observers: {
        'show': function(newVal){
            this.setData({
                newText: this.data.text,
                newImg: this.data.img
            })
        },
        
    },
    /**
     * 组件的方法列表
     */
    methods: {
        //关闭
        close: function() {
            this.triggerEvent('confirm');  
        },
        //保存
        saveText: function() {
            let {newText,tips,isImg,newImg} = this.data;
            if(!newText.trim()){
                wx.showToast({
                    title: tips,
                    icon: 'none'
                })
                return;
            }
            this.triggerEvent('confirm',{change: true, text: newText, img: newImg});  
        },
        //上传图片
        uploadImg: function () {
            let _this = this;
            wx.$utils.uploadImg({count: 1},function(url) {
                _this.setData({
                    newImg: url
                })
            })
        },
        //预览图片
        reviewImg: function () {
            let {newImg} = this.data;
            wx.previewImage({
                current: newImg,
                urls: [newImg]
            })
        },
        //删除图片
        removeImg: function () {
            let _this = this;
            wx.showModal({
                title: '提示',
                content: '确定要删除吗',
                success: function (res) {
                    if (res.confirm) {
                        _this.setData({
                            newImg: ''
                        })
                    }
                }
            })
        },
    }
})